package com.simpleaccount.User;

import android.widget.Toast;

import com.simpleaccount.Toast.TextApplication;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by zhou on 2017/7/17.
 * 用于逐条更新用户的信息。包括：
 *     昵称  email  电话号
 * 从本地操作，修改位于bmob的信息
 */

public class UpdateUserInfo {//update user info
  private String nickName;
    private String email;
    private String phoneNo;



    public void updateNickname(){//update nickname
        AppUser newUser=new AppUser();
        newUser.setNickName(nickName);
        AppUser appUser=BmobUser.getCurrentUser(AppUser.class);
        if(appUser!=null) {//if appuser
            newUser.update(appUser.getObjectId(), new UpdateListener() {//listener
                @Override
                public void done(BmobException e) {//done
                    if (e == null) {//if e null
                        Toast.makeText(TextApplication.getAppContext(),"更新昵称成功",Toast.LENGTH_SHORT).show();
                    }//if e null
                    else {//else
                        Toast.makeText(TextApplication.getAppContext(),"更新昵称失败"+e.getMessage()+e.getErrorCode(),Toast.LENGTH_SHORT).show();
                    }//else
                }//done
            });//listener
        }//if appuser
        else{//else appuser
            //输出未登录信息
            Toast.makeText(TextApplication.getAppContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
        }//else appuser
    }//update nickname
    public void updateEmail(){//update email
        AppUser newUser=new AppUser();
        newUser.setEmail(email);
        AppUser appUser=BmobUser.getCurrentUser(AppUser.class);
        if(appUser != null) {//if appuser
            newUser.update(appUser.getObjectId(), new UpdateListener() {//listener
                @Override
                public void done(BmobException e) {//done
                    if (e == null) {//if e null
                    Toast.makeText(TextApplication.getAppContext(),"更新邮箱失败",Toast.LENGTH_SHORT).show();
                    }//if e null
                    else  {//else
                        Toast.makeText(TextApplication.getAppContext(),"更新邮箱失败",Toast.LENGTH_SHORT).show();
                    }//else
                }//done
            });//listener
        }//if appuser
        else{//else appuser
            //输出未登录信息
            Toast.makeText(TextApplication.getAppContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
        }//else appuser
    }//update email
    public void updatePhoneNo(){//update phoneNo
        AppUser newUser=new AppUser();
        newUser.setMobilePhoneNumber(phoneNo);
        AppUser appUser=BmobUser.getCurrentUser(AppUser.class);
        if (appUser!=null) {//if appuser
            newUser.update(appUser.getObjectId(), new UpdateListener() {//update listener
                @Override
                public void done(BmobException e) {//done
                    if (e == null) {//if e==null
                    Toast.makeText(TextApplication.getAppContext(),"更新电话号码成功",Toast.LENGTH_SHORT).show();
                    }//if e==null
                    else {//else
                    Toast.makeText(TextApplication.getAppContext(),"更新电话号码失败",Toast.LENGTH_SHORT).show();
                    }//else
                }//done
            });//update listener
        }//if appuser
        else{//else appuser
            //输出未登录信息
            Toast.makeText(TextApplication.getAppContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
        }//else appuser
    }//update phoneNo
}//update user info


